/** @file mouse.h
  *
  * Funkcje obslugi myszki
  */

#include "mouse.h"

using namespace std;

Circle* mCircle(MyAllegro &screen){
  static bool tb1=false;
  static Vector2 mvt; // przechowuje miejsce w ktorym wcisnieto lewy przycisk

  if(mouse_b & 1) {
    Vector2 mouseV(mouse_x, mouse_y);

    if(!tb1) {
      tb1 = true;
      mvt = mouseV;
    } else {
        unsigned int d = static_cast<int>(dist(mvt, mouseV));
        circle(screen.getBuffer(), static_cast<int>(mvt.x), static_cast<int>(mvt.y), d, screen.cWhite());
      }
  } else {
      Vector2 mouseV(mouse_x, mouse_y);

      if(tb1) {
        tb1 = false;
        int d = static_cast<int>(dist(mvt, mouseV));

        if(d >= 5)
          return new Circle(mvt, d, screen.cRandom());
      }
    }

  return 0;
}

Poly* mPoly(MyAllegro &screen){
  static vector<Vector2> tab;
  static bool tb1 = false, tb2 = false;
  static Vector2 mvt;
  Vector2 mouseV(mouse_x, mouse_y);

  if(mouse_b & 1) {
    if(!tb1) {
      tb1 = true;
      tab.push_back(mouseV);
    }
  }
  //--1 jesli to zostawimy to normalne wielokaty
  else if(tb1) tb1 = false;

  if(mouse_b & 2){
    if(!tb2)
      tb2 = true;
  } else {
      //--1 a jesli zostawimy to, mozna rysowac dowolne figury odrecznie ze tak powiem ;)
      //if(tb1) tb1 = false;

      if(tb2) {
        tb2=false;

        if(tab.size() >= 3){
          Vector2 *tmp = new Vector2[tab.size()];

          for(unsigned int i = 0; i < tab.size(); i++)
            tmp[i] = tab[i];

          /// @todo Wyznaczanie srodka tworzonego wielokata (teraz jest 0,0)
          Poly *p = new Poly(Vector2(0,0), tab.size(), tmp, screen.cRandom());
          tab.clear();

          return p;
        }
      }
    }

  if(tab.size() >= 2)
    for(unsigned int i=0; i < tab.size()-1; i++){
      line(screen.getBuffer(), tab[i].x, tab[i].y, tab[i+1].x, tab[i+1].y, screen.cWhite());
    }

  if(tab.size() >= 1)
    line(screen.getBuffer(), tab[tab.size()-1].x, tab[tab.size()-1].y, mouseV.x, mouseV.y, screen.cBlack());

  return 0;
}

const float BOMB_FORCE = 300; ///< Sila 'bomby'

void mBombs(vector<Circle*> &tab){
  static bool tb1 = false;
  Vector2 mouseV(mouse_x, mouse_y);

  if(mouse_b & 1) {
    if(!tb1){
      tb1 = true;
      int s = tab.size();

      for(int i = 0; i < s; i++) {
        float d = dist(mouseV, tab[i]->p);
        tab[i]->f = Vector2(-BOMB_FORCE*(mouseV.x-tab[i]->p.x)/d/(d/100),
                            -BOMB_FORCE*(mouseV.y-tab[i]->p.y)/d/(d/100));
      }
    }
  } else
      if(tb1) tb1 = false;
}
